package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpSysParamMapper;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @author System
 */
@Service("upmpSysParamService")
@DS("upmp")
public class UpmpSysParamServiceImpl extends ServiceImpl<UpmpSysParamMapper, UpmpSysParam> implements IUpmpSysParamService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public UpmpSysParam getById(String id){
       return this.baseMapper.selectById(id);
    }
    @Override
    public List<UpmpSysParam> findChildrenByPrimaryKey(String primaryKey, String instId,String parentId) {
        UpmpSysParam parentvo = new UpmpSysParam();
        parentvo.setPrimaryKey(primaryKey);
        parentvo.setInstId(instId);
        parentvo.setParentId(parentId);
        parentvo.setParamStatus(IgasYesOrNoEnum.Yes.getType());
        UpmpSysParam parent = this.baseMapper.findByPrimaryKey(parentvo);
        if(!ObjectUtils.isEmpty(parent)) {
            return this.baseMapper.findEnabledChildrenByParent(parent);
        }
        return null;
    }

    @Override
    public UpmpSysParam findDefaultInst(String primaryKey, String instId, String parentId) {
        return null;
    }

/*    @Override
    public UpmpSysParam findChildrenByPrimaryKey(String primaryKey, String parentPrimaryKey) {
        UpmpSysParam parent = this.baseMapper.findByPrimaryKey(parentPrimaryKey);
        return this.baseMapper.findByPrimaryKeyAndParent(parent,primaryKey);
    }*/

    @Override
    public UpmpSysParam findByPrimaryKey(String parentPrimaryKey, String instId,String parentId) {

        UpmpSysParam parentvo = new UpmpSysParam();
        parentvo.setPrimaryKey(parentPrimaryKey);
        parentvo.setInstId(instId);
        parentvo.setParentId(parentId);
        return this.baseMapper.findByPrimaryKey(parentvo);
    }

    @Override
    public Map<String, String> findMapDataByPrimaryKey(String primaryKey, String instId,String parentId) {
        Map<String, String> dataMap = Maps.newLinkedHashMap();
        UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
        List<UpmpSysParam> dataDicts = findChildrenByPrimaryKey(primaryKey,defaultInst.getPrimaryValue(),parentId);
        for (UpmpSysParam dataDict : dataDicts) {
            dataMap.put(dataDict.getPrimaryKey(), dataDict.getPrimaryValue());
        }
        return dataMap;
    }

    @Override
    public Map<String, UpmpSysParam> findMapObjectByPrimaryKey(String primaryKey, String instId,String parentId) {
        Map<String, UpmpSysParam> dataMap = Maps.newLinkedHashMap();
        List<UpmpSysParam> dataDicts = findChildrenByPrimaryKey(primaryKey,instId,parentId);
        for (UpmpSysParam dataDict : dataDicts) {
            dataMap.put(dataDict.getPrimaryKey(), dataDict);
        }
        return dataMap;
    }

    @Override
    public UpmpSysParam findChildrenByPrimaryKeyAndParent(String primaryKey, String parentPrimaryKey, String instId,String parentParentId) {
        UpmpSysParam parentvo = new UpmpSysParam();
        parentvo.setPrimaryKey(parentPrimaryKey);
        parentvo.setInstId(instId);
        parentvo.setParentId(parentParentId);
        parentvo.setParamStatus(IgasYesOrNoEnum.Yes.getType());
        UpmpSysParam parent = this.baseMapper.findByPrimaryKey(parentvo);
        if(!ObjectUtils.isEmpty(parent)){
            UpmpSysParam sonvo = new UpmpSysParam();
            sonvo.setPrimaryKey(primaryKey);
            sonvo.setInstId(instId);
            sonvo.setParentId(parent.getId());
            sonvo.setParamStatus(IgasYesOrNoEnum.Yes.getType());
            return this.baseMapper.findByPrimaryKey(sonvo);
        }
        return null;
    }
}
